package ds.factcom.view.console.product;

import ds.factcom.controller.product.ProductController;
import ds.factcom.exception.EmptyStringException;
import ds.factcom.exception.ExistDomainException;
import ds.factcom.exception.FloatNumberFormatException;
import ds.factcom.exception.IsNotNumberException;
import ds.factcom.exception.IsProductInInvoicesException;
import ds.factcom.exception.NotExistDomainException;
import ds.factcom.view.console.BaseConsoleView;
import ds.factcom.view.validate.ValidatorProduct;

/**
 * 
 * @author Javier De Luca, Maximiliano Herrera, Matias Samblancat
 *
 */
public class ProductConsoleView extends BaseConsoleView{
	
	private final ProductController productController; 
	
	private String description;
	private Float price;
	private boolean stop = false;
	private String stringAux;
	private Integer selection;
	private boolean desicion = true;
	
	
	//Constructor
	public ProductConsoleView(ProductController productController ){
		this.productController =  productController;
		this.productController.addObserver(this);
	}
	
	public void menuProduct(){
		while (!stop ){
			// Imprimo menu de opciones
			System.out.println("-------------------------------------------------------");
			System.out.println("MENU PRODUCTOS");
			System.out.println("-------------------------------------------------------");
			System.out.println("1.- Alta Producto");
			System.out.println("2.- Modificar Producto");
			System.out.println("3.- Baja producto");
			System.out.println("4.- Volver");
			System.out.println("-------------------------------------------------------");
			System.out.print("Opcion:");
			try {
				
				stringAux = getScanner().nextLine();
				ValidatorProduct.correctSelection(stringAux);
				selection = Integer.valueOf(stringAux);

				switch (selection) {
				case 1:
					this.insertProduct();
					break;
				case 2:
					this.modifyProduct();
					break;
				case 3:
					this.deleteProduct();
					break;
				case 4:
					this.back();
					break;
				default:
					System.out.println(stringAux+" es un valor equivocado");
				}
			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (IsNotNumberException e) {
				showErrorMessage(e.getMessage());
			} 
		}
	}
	
	private void back() {
		this.stop = true;		
	}

	private void deleteProduct(){
		
		this.desicion = true;
		
		System.out.println("-------------------------------------------------------");
		System.out.println("BAJA PRODUCTO");
		System.out.println("-------------------------------------------------------");
		
		while(this.desicion){
			try {
				System.out.print("Indique la descripcion del producto a borrar:");
				this.stringAux = getScanner().nextLine();
				ValidatorProduct.isNotEmpty(this.stringAux);
				this.description = this.stringAux;
				this.productController.delete(description);
			} catch (NotExistDomainException e) {
				showErrorMessage(e.getMessage());
			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (IsProductInInvoicesException e) {
				showErrorMessage(e.getMessage());
			}finally{
				this.getMessageContinue("Eliminar");
			}
		}
	}

	private void modifyProduct() {
		
		desicion = true;
		
		System.out.println("-------------------------------------------------------");
		System.out.println("MODIFICAR PRODUCTO");
		System.out.println("-------------------------------------------------------");
		
		while(desicion){
			try {
				System.out.println("Indique la descripcion del producto a Modificar");
				stringAux = getScanner().nextLine();
				ValidatorProduct.isNotEmpty(stringAux);
				this.description = stringAux;
				this.productController.findByDescription(this.description);
				
				System.out.println("Ingrese el nuevo precio: "+ 
						this.productController.getPrice());
				this.stringAux = getScanner().nextLine();
				ValidatorProduct.isFloat(this.stringAux);
				this.price = Float.valueOf(this.stringAux);
				
				this.productController.modify(price);
				
			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (NotExistDomainException e) {
				showErrorMessage(e.getMessage());
			} catch (FloatNumberFormatException e) {
				showErrorMessage(e.getMessage());
			}finally{
				this.getMessageContinue("Modificar");
			}
		}
	}

	private void insertProduct() {
		
		desicion = true;
		
		System.out.println("-------------------------------------------------------");
		System.out.println("ALTA PRODUCTO");
		System.out.println("-------------------------------------------------------");
		
		while(desicion){
			try{
				System.out.println("Ingrese una Descripci�n: ");
				stringAux = getScanner().nextLine();
				ValidatorProduct.isNotEmpty(stringAux);
				this.description = stringAux;
				
				System.out.println("Ingrese el precio: ");
				stringAux = getScanner().nextLine();
				ValidatorProduct.isFloat(stringAux);
				this.price = Float.valueOf(stringAux);
				this.productController.insert(this.description, this.price);
			} catch (ExistDomainException e) {
				showErrorMessage(e.getMessage());
			} catch (EmptyStringException e) {
				showErrorMessage(e.getMessage());
			} catch (FloatNumberFormatException e) {
				showErrorMessage(e.getMessage());
			}finally{
				this.getMessageContinue("Insertar");
			}
		}
	}
	
	private void getMessageContinue(String action){
		System.out.println("Desea "+action+" otro Producto S/N");
		this.stringAux = getScanner().nextLine();
		if("N".equalsIgnoreCase(this.stringAux)){
			desicion = false;
		}
	}
}
